﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using XiaoweiUWP.HpControls;
using XiaoweiUWP.Models;
using XiaoweiUWP.ViewModels;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace XiaoweiUWP.Pags
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class SkillCenterPage : Page
    {
        public SkillCenterPage()
        {
            this.InitializeComponent();

            this.initialData();
            this.initialEvent();
            PopupView.Closed += PopupView_Closed;
            this.Loaded += SkillCenterPage_Loaded;
        }

        private void SkillCenterPage_Loaded(object sender, RoutedEventArgs e)
        {
          
        }

        private void PopupView_Closed(object sender, object e)
        {    
            if(CurrentSkillItem != null)
            {
                CurrentSkillItem.TurnNormal();
            }
        }
        private SkillItem CurrentSkillItem = null;
        private void initialEvent()
        {
            EventManager.Register("test", (a) =>
            {
                if(a is SkillItem curren)
                {
                    this.CurrentSkillItem = curren;
                }
                SetPopUpPosition(a);
            });
        }
        private ObservableCollection<PopItem> Collection = new ObservableCollection<PopItem>();
        public ObservableCollection<PopItem> PopCollection
        {
            get
            {
                return Collection;
            }
        }
       
        private void initialData()
        {
            Collection.Add(new PopItem("\"现在时间是多少\"", "http://www.baidu.com"));
            Collection.Add(new PopItem("\"我要看股市\"", "http://www.baidu.com"));
            Collection.Add(new PopItem("\"今天天气如何\"", "http://www.baidu.com"));
            Collection.Add(new PopItem("百科搜索", "http://www.baidu.com"));
        }
        private void SetPopUpPosition(UserControl uiControl)
        {
            var t = uiControl.TransformToVisual((UIElement)RootGrid);
            Point screenCoords = t.TransformPoint(new Point(0, 0));
            PopupView.IsOpen = true;
          

                _ = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    if (!IsBeyondTheBottomBoundary((int)screenCoords.X, (int)screenCoords.Y))
                    {
                        CurrentSkillItem?.TurnDown();
                        Canvas.SetLeft(PopupView, screenCoords.X);
                        Canvas.SetTop(PopupView, screenCoords.Y + uiControl.ActualHeight);
                    }
                    else
                    {
                        CurrentSkillItem?.TurnUp();
                    }
                });

            
        }
        private bool IsBeyondTheBottomBoundary(int x, int y)
        {
            //StandardPopup.IsOpen = true;
            var b = false;
            var itemHeight = 40;
            var eleH = this.PopCollection.Count * itemHeight;

            var h = eleH + y;

            if (h > this.ActualHeight)
            {
                Canvas.SetLeft(PopupView, x);
                Canvas.SetTop(PopupView, y - eleH);
                return true;
            }
            return false;
        }
    }
}
